package com.histone.lilian.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.util.StrUtil;
import com.histone.lilian.mapper.AuthMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    private final AuthMapper authMapper;

    public StpInterfaceImpl(AuthMapper authMapper) {
        this.authMapper = authMapper;
    }

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> roleList = getRoleList(loginId, loginType);
        List<String> list;
        if (roleList.contains("admin")) {
           list = new ArrayList<>(1);
           list.add("*:*:*");
           return list;
        }
        list = authMapper.getPermissionList(loginId);
        return list.stream().filter(perm -> StrUtil.isNotBlank(perm)).collect(Collectors.toList());
    }

    /**
     * todo 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return authMapper.getRoleList(loginId);
    }

}